package org.NooLab.itexx.app.gui.handlers;

import java.awt.Color;
import java.awt.event.KeyEvent;

import javax.swing.JTextField;

import org.NooLab.itexx.ITexxWindowCommons;
import org.NooLab.itexx.app.config.ITexxAppProperties;
import org.NooLab.itexx.app.controller.ItexxModelController;
import org.NooLab.itexx.app.gui.definition.ITexxWindowGui;
import org.NooLab.itexx.app.gui.definition.ITexxWindowGuiIntf;
import org.NooLab.itexx.retrieval.RetrievalEngine;
import org.NooLab.itexx.retrieval.RetrievalEngineIntf;
import org.NooLab.itexx.stuctures.RequestItem;
import org.NooLab.utilities.logging.PrintLog;



public class TextFieldHandler {
	
	
	JTextField textField;
	ITexxWindowGuiIntf app;
	ItexxModelController itexxModel;
	private ITexxWindowCommons appCommons;
	
	
	public TextFieldHandler(ITexxWindowGuiIntf app, JTextField textfield) {
		textField = textfield ;
		this.app = app;
		appCommons = app.getItexxModel().getTexxWindowApp().getAppCommons();
		itexxModel = app.getItexxModel();
	}

	public void handling( KeyEvent evt ) {
		String str="";
		boolean  fullRequest, fileContext ;
		
		str = textField.getText() ;
		int kc=evt.getKeyCode();
		
		if (kc==10){
			// release Enter event for "dedicated search"
			
			if ((str.trim().startsWith("dedicated")) && (str.trim().endsWith("..."))){
				return;
			}
			// release event == send message about entered text
			if (textField.isFocusable()){
				if (textField.isFocusOwner()){
					textField.setFocusable(false);
					PrintLog.Delay(20);
					textField.setFocusable(true);
				}
			}
			System.err.println("TextFieldDedicatedSearch, entered text = "+str);
			
			// activate the dispatcher... it won't be activated, 
			// if the browser would be used directly
			
			
			// we came by keyword, so we know that it is a manual request, 
			// file input (even manual) always uses auto delay 
			itexxModel.getSearchRequest().setManualRequest(true) ;
			
			
			// TODO: dependent on option from the GUI, we either prepare a full complex request or a simple one
			String filename = "";
			RequestItem requestItem = RequestItem.prepareRequestItemDedicated(  str, itexxModel) ;
			
			// 
			int pageCount = ITexxAppProperties.getMaxNumberOfSearchEnginePages();
			requestItem.setCountOfNextPages( pageCount ); 
			
			// call with new dedicated request and the retrieval engine, 0=normal, 1=display only the main body (content panel/div ) -1=raw -> direct display of retrieval
											appCommons.out.print(2,"creating new RetrievalEngine, calling updateRequestEntries (1)...");
			itexxModel.getSearchRequest().updateRequestEntries( new RetrievalEngine(itexxModel.getTexxWindowApp(), 1, RetrievalEngineIntf._HOST_GOOGLE ), requestItem) ;
			
		}else{
			boolean inputChanged = false;
			if ((str.trim().startsWith("dedicated")==false) && (str.trim().endsWith("..."))){
				str = str.trim().substring(0,str.indexOf("...")) ;
				inputChanged = true;
			}
			if (str.length()>3){
				int p=str.indexOf("...");
				if (p>=0){
					str = str.substring( p+3,str.length()) ;
					inputChanged = true;
				}
			}
			
			if (inputChanged){
				textField.setText(str);
				textField.repaint() ; // sets the caret to the end

			}
			textField.setForeground(Color.black) ;
			
		}
		
		
	}

}
